/*
 * Protocol.h
 *
 *  Created on: 26-06-2012
 *      Author: Mati
 */
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#ifndef PROTOCOL_H_
#define PROTOCOL_H_

#include "../UART/UART.h"

typedef uint8_t byte;

#define _SOF                  0xFF
// first byte of the command
#define _main 			      0x6D  //m
#define _pid                  0x70   //p
#define _prepareMode          0x67   //g
#define _workMode             0x77   //w
#define _twr				  0x74   //t

//second byte of the command
//main
#define _autoMode             0x61   //a
#define _userMode             0x75   //u
#define _temperature          0x74   //t
#define _current              0x63   //c
#define _voltage              0x76   //v
#define _dac_voltage          0x56   //V
#define _adc_voltage		  0x57
#define _speaker              0x73   //s
#define _debug                0x64   //d
#define _mode			      0x6D   //m
#define _resistance			  0x72	 //r

//pid
#define _kp                   0x6A   //j
#define _ki                   0x69   //i
#define _kd                   0x6B   //k

#define _prec300              0x33   //3
#define _prec20               0x32   //2
#define _precUser             0x75   //u

//prepareMode
#define _ptime1               0x31   //1
#define _ptime2               0x32   //2
#define _ptime3               0x33   //3
#define _ptime4               0x34   //4
#define _ptime5               0x35   //5

#define _ptemp1               0x61   //a
#define _ptemp2               0x62   //b
#define _ptemp3               0x63   //c
#define _ptemp4               0x64   //d
#define _ptemp5               0x65   //e

//workMode
#define _wtime1               0x31   //1
#define _wtime2               0x32   //2
#define _wtemp1               0x61   //a
#define _wtemp2               0x62   //b
//twr
#define _A_TWR				  0x61	//a
#define _B_TWR				  0x62	//b


// ------------------ addresses ------------------------------
//second byte of the command
//main
#define _addr_autoMode             0x30
#define _addr_userMode             ((_addr_autoMode) + sizeof(uint8_t))
#define _addr_speaker              ((_addr_userMode) + sizeof(uint8_t))
#define _addr_debug                ((_addr_speaker) + sizeof(uint8_t))

//pid
#define _addr_kp                   ((_addr_debug) + sizeof(uint8_t))
#define _addr_ki                   ((_addr_kp) + sizeof(uint16_t))
#define _addr_kd                   ((_addr_ki) + sizeof(uint16_t))

#define _addr_prec300              ((_addr_kd) + sizeof(uint16_t))
#define _addr_prec20               ((_addr_prec300) + sizeof(uint8_t))
#define _addr_precUser             ((_addr_prec20) + sizeof(uint8_t))

//prepareMode
#define _addr_ptime1               ((_addr_precUser) + sizeof(uint8_t))
#define _addr_ptime2               ((_addr_ptime1) + sizeof(uint16_t))
#define _addr_ptime3               ((_addr_ptime2) + sizeof(uint16_t))
#define _addr_ptime4               ((_addr_ptime3) + sizeof(uint16_t))
#define _addr_ptime5               ((_addr_ptime4) + sizeof(uint16_t))

#define _addr_ptemp1               ((_addr_ptime5) + sizeof(uint16_t))
#define _addr_ptemp2               ((_addr_ptemp1) + sizeof(uint16_t))
#define _addr_ptemp3               ((_addr_ptemp2) + sizeof(uint16_t))
#define _addr_ptemp4               ((_addr_ptemp3) + sizeof(uint16_t))
#define _addr_ptemp5               ((_addr_ptemp4) + sizeof(uint16_t))
//workMode
#define _addr_wtime1               ((_addr_ptemp5) + sizeof(uint16_t))
#define _addr_wtime2               ((_addr_wtime1) + sizeof(uint16_t))
#define _addr_wtemp1               ((_addr_wtime2) + sizeof(uint16_t))
#define _addr_wtemp2               ((_addr_wtemp1) + sizeof(uint16_t))
//twr
#define _addr_A_TWR               ((_addr_wtemp2)  + sizeof(uint16_t))
#define _addr_B_TWR               ((_addr_A_TWR)   + sizeof(uint16_t))

typedef struct {
        const byte SOF;
        // first byte of the command
        const byte main;
        const byte pid;
        const byte prepareMode;
        const byte workMode;
        const byte twr;

        //second byte of the command
        //main
        const byte autoMode;
        const byte userMode;
        const byte temperature;
        const byte current;
        const byte voltage;
        const byte dac_voltage;
        const byte adc_voltage;
        const byte speaker;
        const byte debug;
        const byte resistance;
        const byte mode;

//        //pid
        const byte kp;
        const byte ki;
        const byte kd;

        const byte prec300;
        const byte prec20;
        const byte precUser;

//        //prepareMode
        const byte ptime1;
        const byte ptime2;
        const byte ptime3;
        const byte ptime4;
        const byte ptime5;

        const byte ptemp1;
        const byte ptemp2;
        const byte ptemp3;
        const byte ptemp4;
        const byte ptemp5;

        //workMode
        const byte wtime1;
        const byte wtime2;
        const byte wtemp1;
        const byte wtemp2;

        //twr
        const byte A_TWR;
        const byte B_TWR;
}  ProtocolMarkers;

//ProtocolMarkers PMarker;
extern ProtocolMarkers PMarker;

typedef struct {
        //second byte of the command
        //main
        uint8_t autoMode;
        uint8_t userMode;
        uint16_t temperature;
        uint16_t current;
        uint16_t voltage;
        uint16_t dac_voltage;
        uint8_t speaker;
        uint8_t debug;
        uint16_t resistance;

        //pid
//        uint8_t kpMSB;
//        uint8_t kpLSB;
//        uint8_t kiMSB;
//        uint8_t kiLSB;
//        uint8_t kdMSB;
//        uint8_t kdLSB;

        float kp;
        float ki;
        float kd;

        uint8_t prec300;
        uint8_t prec20;
        uint8_t precUser;

        //prepareMode
        uint16_t ptime1;
        uint16_t ptime2;
        uint16_t ptime3;
        uint16_t ptime4;
        uint16_t ptime5;

        uint16_t ptemp1;
        uint16_t ptemp2;
        uint16_t ptemp3;
        uint16_t ptemp4;
        uint16_t ptemp5;

        //workMode
        uint16_t wtime1;
        uint16_t wtime2;
        uint16_t wtemp1;
        uint16_t wtemp2;

        //twr
        float A_TWR;
        float B_TWR;
}  ProtocolValues;

extern ProtocolValues PVal;

typedef struct {
        //second byte of the command
        //main
        uint8_t addr_autoMode;
        uint8_t addr_userMode;
        uint8_t addr_speaker;
        uint8_t addr_debug;

        //pid
        uint8_t addr_kp;
        uint8_t addr_ki;
        uint8_t addr_kd;

        uint8_t addr_prec300;
        uint8_t addr_prec20;
        uint8_t addr_precUser;

        //prepareMode
        uint8_t addr_ptime1;
        uint8_t addr_ptime2;
        uint8_t addr_ptime3;
        uint8_t addr_ptime4;
        uint8_t addr_ptime5;

        uint8_t addr_ptemp1;
        uint8_t addr_ptemp2;
        uint8_t addr_ptemp3;
        uint8_t addr_ptemp4;
        uint8_t addr_ptemp5;

        //workMode
        uint8_t addr_wtime1;
        uint8_t addr_wtime2;
        uint8_t addr_wtemp1;
        uint8_t addr_wtemp2;
        //twr
        uint8_t addr_A_TWR;
        uint8_t addr_B_TWR;

}  ProtocolAddress;

extern ProtocolAddress PAddress;

void Send(uint8_t parameter, uint8_t *pVal);
void SendBufferThruUART(uint8_t *buffer, uint8_t size);
float ConvertUInt16ToFloat(uint16_t val);
uint16_t ConvertFloatToUInt16(float fval);

#endif /* PROTOCOL_H_ */
